package cn.zzp.pet.controller;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.util.JsonResult;
import cn.zzp.basic.util.LoginContext;
import cn.zzp.basic.util.PageList;
import cn.zzp.pet.domain.SearchMasterMsg;

import cn.zzp.pet.dto.AcceptDto;
import cn.zzp.pet.query.SearchMasterMsgQuery;
import cn.zzp.pet.service.SearchMasterMsgService;
import cn.zzp.user.domain.LoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/17 10:24
 */
@Api(tags = "寻主模块")
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private SearchMasterMsgService searchMasterMsgService;

    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<SearchMasterMsg> queryAll() {
        return searchMasterMsgService.queryAll();
    }

    /**
     * 发布寻主消息
     *
     * @param searchMasterMsg
     * @return
     */
    @ApiOperation(value = "发布消息")
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);

            searchMasterMsgService.publish(searchMasterMsg, loginInfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 根据id查询一条数据
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "Long", required = true, value = "用户Id")
    })
    @ApiOperation(value = "根据id查询一条数据")
    @GetMapping("/{id}")
    public SearchMasterMsg queryOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.queryById(id);
    }

    /**
     * 删除操作
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 添加或修改
     *
     * @param searchMasterMsg
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() == null) {
                searchMasterMsgService.add(searchMasterMsg);
            } else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 分页高级查询
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "分页高级查询")
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            return searchMasterMsgService.pageQuery(query, request);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 拒单
     *
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 接单
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/accept")
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request) {
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.accept(dto, loginInfo);
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

}
